package com.dawnClass.controller;

import com.dawnClass.VO.OrderOrderDetailVO;
import com.dawnClass.config.RabbitMQConfiguration;
import com.dawnClass.pojo.Consumer;
import com.dawnClass.pojo.ConsumerOrder;
import com.dawnClass.pojo.Course;
import com.dawnClass.service.BuyService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class BuyController {

    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private BuyService buyService;

    @GetMapping("buy/{courseId}")
    public ModelAndView getById(@PathVariable("courseId") Integer courseId , HttpSession session){
        System.out.println("session.getAttribute(\"ServiceResponse\"):"+session.getAttribute("ServiceResponse"));
        Course course = buyService.getById(courseId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("course",course);
        mav.setViewName("pay/buy");
        return mav;
    }

    @PostMapping("createOrder")
    @ResponseBody
    public ModelAndView save(Integer courseId , Float orderTotal , HttpSession session){
        ServiceResponse serviceResponse = (ServiceResponse) session.getAttribute("ServiceResponse");
        Consumer loginConsumer = (Consumer) serviceResponse.getdata();
        ModelAndView mav = new ModelAndView();
        //先判断是否购买过当前课程
        ServiceResponse selectResponse = buyService.selectOrder(loginConsumer.getConsumerId(), courseId);
        if (selectResponse.getCode() == 200){
            //存在已购买或着正在购买课程
            if (selectResponse.equals("已购买")){
                mav.addObject("courseId",((ConsumerOrder)selectResponse.getdata()).getCourseId());
                mav.setViewName("pay/paid");
                return mav;
            }
            mav.addObject("second","second");
            mav.addObject("orderResponse",selectResponse);
            mav.setViewName("pay/toPay");
            return mav;
        }

        // 1. 调用service 保存 order对象
        ServiceResponse orderResponse = buyService.addOrder(loginConsumer.getConsumerId(),courseId,orderTotal);
        String orderNum = ((OrderOrderDetailVO)orderResponse.getdata()).getOrder().getOrderNum();

        rabbitTemplate.convertAndSend(RabbitMQConfiguration.orderExchange,
                RabbitMQConfiguration.routingKeyOrder,

                // 如果配置了 params.put("x-message-ttl", 5 * 1000);
                // 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
                orderNum,message -> {
                    message.getMessageProperties().setExpiration(1000 * 300 + "");
                    return  message;
                });

        if(orderResponse.getCode() == 200){  // 添加成功

            //3. 绑定数据到ModelAndView中，页面跳转
            mav.addObject("orderResponse",orderResponse);
            mav.setViewName("pay/toPay");
        }else{// 添加失败， 跳转到 添加页面
            mav.addObject("addMessage","添加失败");
            mav.setViewName("pay/buy");
        }
        return mav;
    }
}
